<template>
	<view class="page" v-if="!loadding">
		<!-- <view class="head">
			<view class="head-img">
				<image src="../../../static/jiaotou1.png" @click="gotoPage('/pages/user/index/index')"></image>
			</view>
			<view class="title">编辑资料</view>
			
		</view> -->
		<view>
			
			<view class="vip">
				<text>头像:</text>
				<view class="vipmsg" @click="chooseImages('head_portrait')">
					<view class="upload-box">
						<view class="upload-item">
							<view class="upload-icon" v-if="userinfo.avatarUrl == ''">
								<image class="portrait" src="../../../static/dafaulthead2.png"></image>
							</view>
							<image class="Newportrait" :src="userinfo.avatarUrl" mode="" v-else></image>
						</view>
					</view>
					<image class="next" src="../../../static/jiantou3.png"></image>
				</view>				
			</view>
			<!-- <view class="vip">
				<text>会员ID:</text>
				<view class="">
					<input type="text" v-model="userinfo.user_id" disabled="" />
				</view>
			</view> -->
		<!-- 	<view class="vip">
				<text>昵称:</text>
				<view class="">
					<input type="text" v-model="userinfo.nickName" />
				</view>
			</view> -->
			<view class="vip">
				<text>手机号:</text>
				<view class="prime"  >
					<input type="text" v-model="userinfo.mobile" disabled="" />
					<image class="next" src="../../../static/jiantou3.png" @click="gotoPage('/pages/user/updateinfo/updateinfo_phone/updateinfo_phone')"></image>
				</view>				
			</view>
			<button class="update" @click="$noMultipleClicks(doUpdate)">确定</button>
		</view>
	</view>
</template>

<script>
	import mpvueCityPicker from '@/components/mpvue-citypicker/mpvueCityPicker.vue';
	var sourceType = [
		['camera'],
		['album'],
		['camera', 'album']
	]
	export default {
		components: {
			mpvueCityPicker
		},
		data() {
			return {
				loadding:true,
				userinfo: '',
				imageList: {
					head_portrait: '',
				},
				noClick:true,
			}
		},
		onLoad() {
			uni.getStorage({
				key:'user_id',   // 储存在本地的变量名
				success:res => {
					// 成功后的回调
					console.log(res);   // hello  这里可做赋值的操作
				}
			})
		},
		onShow() {
			this.getData();
		},
		methods: {
			/* 页面跳转 */
			gotoPage(path) {
				let self = this;
				if (path.startsWith("/")) {
					uni.navigateTo({
						url: path
					});
				} else {
					self[path]();
				}
			},
			/* 获取数据 */
			getData() {
				let self = this;
				uni.showLoading({
					title: '加载中'
				});
				self._get('user.index/setting', {}, function(res) {
					self.userinfo = res.data.userInfo;
					console.log(self.userinfo)
					self.loadding=false;
					uni.hideLoading();
				});
			},
		
			/* 上传图片 */
			chooseImages(type) {
				let self = this;
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: (res) => {
						self.uploadFile(res.tempFilePaths[0], type);
					}
				})
			},
			uploadFile: function(filePath, type) {
				let self = this;
				let i = 0;
				let params = {
					token: uni.getStorageSync('token'),
					app_id: self.getAppId()
				};
				uni.showLoading({
					title: '图片上传中'
				});
				uni.uploadFile({
					url: self.websiteUrl + '/index.php?s=/api/file.upload/image',
					filePath: filePath,
					name: 'iFile',
					formData: params,
					success: function(res) {
						let result = typeof res.data === 'object' ? res.data : JSON.parse(res.data);
						if (result.code === 1) {
							let file_path = result.data.file_path;
							self.imageList[type] = file_path;
							self.userinfo.avatarUrl = file_path;
							
						}
					},
					complete: function() {
						i++;
						uni.hideLoading();
					}
				});
			},
			doUpdate() {
				let self = this;
				//console.log(self.imageList)
				let doupdate = {};
				doupdate.updateinfo_portrait = self.imageList.head_portrait;
				doupdate.updateinfo_phone = self.userinfo.mobile;
				console.log(doupdate)
				//return;
				if (doupdate != '') {
					self._post(
						'user.index/updateinfo',
						doupdate,
						result => {
							if (result.code == 1) {
								uni.showLoading({
									title: '成功！',
									duration: 2000,
									icon: 'none'
								});
								self.gotoPage('/pages/user/index/index');
							}
						},
						false,
						() => {
							uni.hideLoading();
						}
					)
				}
			},

		}
	}
</script>

<style lang="scss">
	@import 'updateinfo.scss';
</style>
